<template>
  <vue-content>
    <vue-header>
      <vue-header-form>
        <el-form :inline="true" :model="formFilter" ref="formFilter">
          <el-form-item label="电子券名称" prop="institutionNo">
            <el-input v-model.trim="formFilter.institutionNo" style="width:150px"></el-input>
          </el-form-item>
          <el-form-item label="类型" prop="institutionName">
            <el-input v-model.trim="formFilter.institutionName" style="width:150px"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="SearchSubmit('formFilter')">查询</el-button>
            <el-button @click="resetForm('formFilter')">重置</el-button>
          </el-form-item>
        </el-form>
      </vue-header-form>
      <vue-header-button>
        <el-button @click="accredit">前往授权</el-button>
        <el-button type="primary" @click="create">新增</el-button>
      </vue-header-button>
    </vue-header>
    <vue-body>
      <div class="content">
        <div class="right_content">
          <el-table
            v-loading.body="loading"
            :data="tableData"
            border
            style="width: 100%">
            <el-table-column
              prop="id"
              label="电子券名称">
            </el-table-column>
            <el-table-column
              prop="specName"
              label="类型">
            </el-table-column>
            <el-table-column
              prop="parentId"
              label="抵用券限制金额">
            </el-table-column>
            <el-table-column
              label="面值">
              <template scope="scope">
                {{ scope.row.parentName || "一级标签" }}
              </template>
            </el-table-column>
            <el-table-column
              prop="parentId"
              label="电子券状态">
            </el-table-column>
            <el-table-column
              prop="parentId"
              label="总数量">
            </el-table-column>
            <el-table-column
              prop="parentId"
              label="有效期结束日期">
            </el-table-column>
            <el-table-column label="操作" width="150">
              <template scope="scope">
                <el-button
                  :disabled="scope.row.status === '1'?true:false"
                  size="small"
                  @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button
                  :disabled="scope.row.status === '1'?true:false"
                  size="small"
                  type="danger"
                  @click="handleDelete(scope.$index, scope.row, tableData)">删除</el-button>
                </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </vue-body>
    <vue-footer style="padding: 5px 0;margin: 0 -5px;float:right;">
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="10"
        layout="total, prev, pager, next"
        :total="pageSize">
      </el-pagination>
    </vue-footer>
    <edit-role
      ref="edit-role"
      @close="editFinish"
      :view="view"
      v-if="view ==='create' || view ==='edit'"/>
  </vue-content>
</template>

<script>
	import {queryspec,query,create,edit,del} from '@/services/coupons'
  import {RequestError} from '@/services/errors'
  import {deepClone} from '@/utilities/object'
  import EditRole from './edit'
  import EditDetail from './Detail'
  export default {
    name: 'role-table',
    components: {EditRole,EditDetail},
    data: () => ({
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      formFilter: {
      },
      loading: false,
      view: '',
    }),
    methods: {
      async loadData () {
        this.loading = true
        try {
          let data = await query(Object.assign({currentPage: this.currentPage},{perPage:10},this.formFilter))
          // debugger
          this.tableData = data.specList
          this.pageSize = parseInt(data.totalResult)
        } catch (e) {
          if (e instanceof RequestError) {
            this.$alert(e.message, '提示', {
              type: 'error',
              confirmButtonText: '确定'
            })
          } else {
            throw e
          }
        }
        this.loading = false
      },
      async create () {
        this.view = 'create'
        await this.$nextTick()
        this.$refs['edit-role'].loadSelect()
      },
      async handleEdit (index, data) {
        // debugger
        this.view = 'edit'
        await this.$nextTick()
        this.$refs['edit-role'].dialogFormVisible = true
        this.$refs['edit-role'].loadSelect();
        this.$refs['edit-role'].formData = deepClone(data)
        this.$refs['edit-role'].id = data.id
      },
      handleDelete (index, data, rows) {
        this.$confirm(`此操作将删除${data.specName}, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          try {
            await del(data.id)
            this.$message({
              type: 'success',
              message: '已删除'
            })
            rows.splice(index, 1)
          } catch (e) {
            if (e instanceof RequestError) {
              this.$alert(e.message, '提示', {
                type: 'error',
                confirmButtonText: '确定'
              })
            } else {
              throw e
            }
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      SearchSubmit () {
        this.loadData()
      },
      accredit(){
        this.$router.push({
	        path: 'coupons/accredit', 
	        name: 'accredit',
	        query: { 
          }
        })
      },
      resetForm (formName) {
        this.$refs[formName].resetFields()
        this.currentPage = 1
        this.loadData()
      },
      editFinish (needRefresh) {
        this.view = ''
        if (needRefresh) {
          this.loadData()
        }
      }
    },
    watch: {
      currentPage (newVal) {
        this.loadData(newVal)
      }
    },
    filters: {
      productType (value) {
        return {1: '实物', 2: '彩票',3:'券/虚拟'}[value]
      },
      distributionStatus (value) {
      	//配送状态
        return {'01': '待发货', '02': '已发货'}[value]
      },
      payType (value) {
      	//支付方式
        return {'01': '微信支付', '02': '芸券支付','03':'兴业银行'}[value]
      },
      isLogistics(value) {
      	//是否需要物流
        return {'01': '需要', '02': '不需要'}[value]
      },
    },
    created () {
      this.loadData()
    }
  }
</script>
<style>

</style>
